<?php
class info extends common{
	
	function __construct(){	
		$pagec = new page($_GET['page']);
		parent::__construct($pagec);
	}	
	private $table_label = 'label';  	  		 	//标签表
	private $table_info_class = 'info_class';    	//分类表
	private $table_info = 'info';    				//咨询表
	private $table_info_label = 'info_label';   	//咨询标签关联表
	private $table_admin_topic='admin_topic';		//小编说表
	private $table_admin='admin';					//管理员

	private $table_comment='info_comment';			//评论表
	private $table_user = 'user';					//用户表
	/**
	 *分类表
	 * @author	jsy
	 * @param	string	$where
	 * @param	string	$key 
	 * @return 	array
	 */
	function info_class_list($where='',$key="i.*"){
		$sql = $this->table_info_class." i WHERE 1=1 ".$where;
		$res = $this->getList($sql,'',$key);
		return $res ;
	}

	/**
	 *标签表
	 * @author	jsy
	 * @param	string	$where
	 * @param	string	$key 
	 * @return 	array
	 */
	function label_list($where='',$key="i.*"){
		$sql = $this->table_label." i WHERE 1=1 ".$where;
		$res = $this->getList($sql,'',$key);
		return $res ;
	}



	/**
	 * 咨询添加
	 * @author	jsy	
	 * @param	array	$arr 		 //单条数组
	 * @param	array	$list 		//批量数组
	 * @return 	bool
	 */
	function info_add($arr,$list){
		if( !empty($arr)){
			$res = $this->insert($this->table_info,$arr);	
			$id = $this->insert_id();	

			if($id>0 && !empty($list)){
				$sql = ' INSERT INTO '.$this->table_info_label. '(info_id,label_id) VALUES ';
				foreach($list as $v){
					$sql.='(';
					$sql.= $id.",".$v['label_id'];
					$sql .= '),';
					$label_id.=$v['label_id'].',';
				}
				$sql = substr($sql,0,-1);
				$res = $this->query($sql);


				//更新标签使用量
				$label_id=substr($label_id,0,-1);
				$update_sql='UPDATE '.$this->table_label.' SET hot_num=hot_num+1 WHERE id in ('.$label_id.')';
				
				$res = $this->query($update_sql);
			}	
		}
		return $res;
	}


	/**
	 * 权限修改
	 * @author	jsy
	 * @param 	int	 	$id
	 * @param	array	$arr  		//单条数组
	 * @param	array	$list 		//批量数组
	 * @return	bool
	 */
	function info_update($id,$arr,$list){
		if( is_array($arr)){
			$res=$this->update($this->table_info,$arr,"id=".$id);
			//删除原有数据
			$res1 = $this->del($this->table_info_label,$id.',','',"info_id");

			if($id && !empty($list)){
				$sql = ' INSERT INTO '.$this->table_info_label. '(info_id,label_id) VALUES ';
				foreach($list as $v){
					$sql.='(';
					$sql.= $id.",".$v['label_id'];
					$sql .= '),';
				}
				$sql = substr($sql,0,-1);
				$res = $this->query($sql);
			}	
		}
		return $res;
	}


	/**
	 * 咨询详情
	 * @author	jsy
	 * @param	int	    $id
	 * @param	string	$key
	 * @return	array
	 */
	function info($id,$key="*"){
		if($id != null && $id != ""){
			$row = $this->getInfo($this->table_info,$id,$key,'id');		
		}
		return $row;
	}


	/**
	 *咨询列表
	 * @author	jsy
	 * @param	string	$where
	 * @param	string	$key 
	 * @return 	array
	 */
	function info_list($where='',$key="i.*,a.group_id"){
		$sql = $this->table_info." i LEFT JOIN ".$this->table_admin." a ON i.user_id=a.id WHERE 1=1 ".$where."  ORDER BY i.create_time desc";

		$res = $this->getList($sql,$sql,$key);
		return $res;
	}


	/**
	 *咨询删除
	 * @author	jsy
	 * @param	string	$ids 
	 * @return 	array
	 */
	function info_del($ids){
		if($ids != null && $ids != ""){
			$delFile = array(
				array('key'=>'thumbnail','dir'=>'../upload/info/large/'),
				array('key'=>'thumbnail','dir'=>'../upload/info/small/'),
			);

			//对应关联表
			$res=$this->del($this->table_info,$ids,$delFile);	
			$res=$this->del($this->table_info_label,$ids,'','info_id');	
		}
		return $res;
	}

	/**
	 * 小编说列表
	 * @param  string  $where [description]
	 * @param  string  $key   [description]
	 * @param  integer $page  [description]
	 * @return [type]         [description]
	 */
	function topicList($where='',$key='*',$page=0){
		$sql=$this->table_admin_topic.' t left join '.$this->table_admin.' a on t.admin_id=a.id 
			  where 1=1 '.$where;
		$sqlnum=$page==0?'':$sql;
		$list=$this->getList($sql,$sqlnum,$key);
		return $list;
	}


	/**
	 * 小编回复
	 * @author	jsy
	 * @param	int		$id  
	 * @param	string	$where  
	 * @param	string	$key 
	 * @param	int	$is_page  
	 * @return 	array
	 */
	function comment_list($id,$where='',$key="a.*,u.account,u1.account as account1",$is_page=false){
		$sql = $this->table_comment." a LEFT JOIN ".$this->table_user." u ON a.user_id=u.id  LEFT JOIN ".$this->table_user." u1 ON a.reply_id=u1.id WHERE a.info_id=".$id.$where." order by a.id desc" ;
		if($is_page){
			$sql_num = $sql;
		}else{
			$sql_num = "";
		}
		$res = $this->getList($sql,$sql_num,$key);
		return $res ;
	}
	

}
?>